<?php
/*
	myEolin/index.php/getMyEolin_php() 에서 불려짐
	자동 변수 목록 여기서 없는 함수들은 트레이스는 위경로를 찾아보자 ㅠㅠ
	$pluginURL, $service,$blogid,$entry,$defaultURL,$database,$blogURL;
*/
global $Euserid,$page,$search;
$Euserid = $data['userid'];
$page =(!empty($_GET['page']))?$_GET['page']:1;
$total = 0;
$search = (!empty($_GET['search']))?$_GET['search']:"";

if( !function_exists('getBookmarkData')){
	function getBookmarkData($page){
		global $Euserid ,$search;
		$myEolinUrl = eolinURL()."/myEolin/getMyBookmark.php?mode=mybm&userid=$Euserid&page=$page&search=".rawurlencode($search)."&time=".time();
		requireComponent('Eolin.PHP.HTTPRequest');		
		$request = new HTTPRequest($myEolinUrl);
		if ($request->send()){
			$tmp = $request->responseText;
			 return fetchBookmarkVal($tmp);
		}
		return null;
	}
}
if( !function_exists('fetchBookmarkVal')){
	function fetchBookmarkVal( $DATA ){
		global $total;
		$xmls = new XMLStruct();
		$outVal = array();
		if( ! $xmls->open($DATA) ) {
			unset($xmls);	
			return null;
		}
		if( is_null(  $xmls->selectNodes('/bookmark/item') )){
			unset($xmls);	
			return null;
		}
	
		
		$tmp = $xmls->selectNode('/bookmark/count');
		$total = $tmp['.value'];
	
		$i =0;
		foreach ($xmls->selectNodes('/bookmark/item') as $field) {
			$outVal[$i]['no'] = $field['no'][0]['.value'] ;
			$outVal[$i]['title'] = $field['title'][0]['.value'] ;
			$outVal[$i]['permalink'] = $field['permalink'][0]['.value'] ;
			$outVal[$i]['description'] = $field['description'][0]['.value'] ;
		$i++;
		}
		unset($xmls);	
		return ( $outVal);
	}
}
if( !function_exists('MyEolinpaging')){
	function MyEolinpaging(){
		global $total,$page;
		$cnt = 10;
		$totalpage = ceil($total /  $cnt);
		if($page > 1) echo "<a href='#void' onclick='prevpage(".($page -1).")'><< 이전페이지</a>";
		else echo "<< 이전페이지";
		echo " | ";
		if($page < $totalpage) echo "<a href='#void' onclick='nextpage(". ($page+1).")'>다음페이지 >></a>";
		else echo "다음페이지 >>";
	
	}
}
$bmdata = getBookmarkData($page);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>이올린 &gt; 내 북마크</title>
	<style type="text/css">
	body                { background-color:#fff; color:#555; font-family:돋움, Verdana, sans-serif; font-size:0.75em; margin:0;}
	a:link, a:visited   { color:#555; text-decoration:none;}
	a:hover, a:active   { color:#336699; text-decoration :underline;}
	hr                  { display:none;}
	*                   { margin:0; padding:0; border:0;}
	li                  { list-style:none;}
	h1                  { font-size:160%}
	h3                  { font-size:120%; letter-spacing:-1px;}
	</style>
	<link rel="stylesheet" type="text/css" media="screen" href="<?php echo $pluginURL?>/plugin-main.css" />
	<script language="JavaScript" type="text/javascript">
	<!--
	var insertData = new Array();
	function checkbm(no){
		rid = document.getElementById("checkData").value;
		if(rid =="" ){
			document.getElementById("checkData").value = no;
		}else{
			newtmp = new Array();
			tmp = rid.split(",");	
			chk = true;
			j=0;
			for(i=0;i<tmp.length;i++){
				if(no == tmp[i])	{
					chk = false;
				}else{
					newtmp[j] = tmp[i];
					j++;
				}
			}
			if(chk) {
				document.getElementById("checkData").value = rid+","+no;
			}else{
				document.getElementById("checkData").value = newtmp.join();
			}
		}
	}

	function insertHtml(){
		var insertHTML ="";
		var templateHTML0 = "<a href='permalink' target='_blank'>title</a>\n"; 
		var templateHTML1 = "<li ><a href='permalink' target='_blank'>title</a></li>\n"; 
		var templateHTML2 = "<dl><dt><a href='permalink' target='_blank'>title</a></dt><dd>desc</dd></dl>\n"; 


		var rid = document.getElementById("checkData").value;
		if (rid != "")
		{
			var tmp = rid.split(",");
			if(document.myform.insertType[0].checked) instype = "0";
			if(document.myform.insertType[1].checked) instype = "1";
			if(document.myform.insertType[2].checked) instype = "2";

			if(tmp.length > 0){
				for(i = 0; i < tmp.length;i++){
					no = tmp[i];
					title = document.getElementById("title"+no).value;
					desc = document.getElementById("desc"+no).value;
					permalink = document.getElementById("permalink"+no).value;
					if(instype == "0") tmpText = templateHTML0;
					else if(instype == "1")		tmpText = templateHTML1;
					else tmpText = templateHTML2;
					tmpText = tmpText.replaceAll("title", title);
					tmpText = tmpText.replaceAll("desc", desc);
					tmpText = tmpText.replaceAll("permalink", permalink);
					insertHTML += tmpText;
				}
				if(instype == "1")		insertHTML =	"<ul>"+insertHTML+"</ul>";;
				opener.addBookMarkInfo(insertHTML);
				alert('글쓰기 창에 북마크정보가 추가되었습니다');
			}
		}else{
			alert('글쓰기 창에 넣을 북마크를 선택하세요');
		}
	}

	function searchForm(){
		if(document.myform.search.value == ""){
			alert("검색어를 입력하세요");
			return false;
		}
		document.myform.submit();
	}

	function nextpage(page){
		document.myform.page.value =page;
		document.myform.submit(); 
	}

	function prevpage(page){
		document.myform.page.value =page;
		document.myform.submit(); 
	}

	if(!String.prototype.replaceAll) {
		String.prototype.replaceAll = function(source, target) {
			source = source.replace(new RegExp("(\\W)", "g"), "\\$1");
			target = target.replace(new RegExp("\\$", "g"), "$$$$");
			return this.replace(new RegExp(source, "gm"), target);
		}
	}
	//-->
	</script>
</head>
<body >

<form action="<?php echo $blogURL;?>/plugin/getMyEolin/" method="get" name="myform" onsubmit="return searchForm();">
<input type="hidden" name="page" value="" />
<input type="hidden" id="checkData" value="" />
<div class="getMyBookmark">
<table width="100%" border="0">
<tr>
	<td>
	<strong><a href="<?php echo eolinURL();?>/my/?mode=mybm" target=_blank>이올린 내 북마크 목록</a></strong>
	</td>
	<td>
	<div class="search">
		검색 : <input type="text" name="search" value="<?php echo $search;?>" class="searchname" />
		<input type="button" value="검색" class="btn_bg_white" onclick="return searchForm();" />
		<input type="button" value="목록" class="btn_bg_white"  onclick="document.myform.search.value ='';document.myform.submit();" />
	</div>
	<td>
	</td>
	</tr>
</table>
<table width="100%" border="0" style=" border-top:1px solid #ccc; border-bottom:1px solid #ccc; ">
					<tr>
						<th class="check">선택</th>
						<th class="subject">제목</th>
					</tr>
<?php
if(!is_null($bmdata)){
	$_eolinURL = eolinURL();
	foreach($bmdata as $item){
		$desc = UTF8::lessen($item['description'],64,"..");
		
		echo "<tr>".CRLF;
		echo "<td align=center valign=top>";
		echo "<input type=checkbox name=no value=\"".$item['no']."\" onclick='checkbm(this.value);'  class='check' />";
		echo "<input type=hidden id='title".$item['no']."' value=\"".htmlspecialchars($item['title'])."\" />";
		echo "<input type=hidden id='desc".$item['no']."' value=\"".htmlspecialchars($item['description'])."\" />";
		echo "<input type=hidden id='permalink".$item['no']."' value=\"$_eolinURL/click/bookmark/?".htmlspecialchars($item['permalink'])."\" />";
		echo "</td>".CRLF;
		echo "<td class='list_subject'><h4><a href='$_eolinURL/click/bookmark/?".$item['permalink']."' target=_blank>".$item['title']."</a></h4>";
		echo "<span class=list_desc>".$desc."</span></td>".CRLF;
		echo "</tr>".CRLF;
	}
}else{
		echo "<tr>".CRLF;
		echo "<td align=center valign=top>";	
		echo "</td>".CRLF;		
		echo "<td class='list_subject' >";		
		echo "<span class=list_desc>이올린 아이디가 존재하지 않거나, 북마크가 없습니다</span></td>".CRLF;
		echo "</tr>".CRLF;		
}
?>
</table>

	<div class="paging"><?php echo MyEolinpaging();?></div>
	<div class="present">북마크 표시 :	<input type="radio" id="insertType1"  name="insertType" value="0" checked class="radiocheck" /> <label for="insertType1">제목 링크(텍스트)</label> <input type="radio" id="insertType2" name="insertType" value="1"  class="radiocheck" /> <label for="insertType2">제목만 표시(목록)</label>
	<input type="radio" id="insertType3" name="insertType" value="2" class="radiocheck"  /> <label for="insertType3">제목과 요약글 표시</label>  </div>
	<div class="submit">
		<input type="button" value="확인" class="btn_bg_big" onclick="insertHtml();" />
		<input type="button" value="닫기" class="btn_bg_big" onclick="self.close();" />
	</div>
	<div style="font-size:11px;text-align:center;color:gray;">* 비공개 북마크나 내 북마크 목록을 비공개로 선택한 경우는 표시되지 않습니다</div>	
</div>
</form>
</body>
</html>
